//
//  MPTVideoDriver.h
//  MiaoPaiSDKDemo
//
//  Created by Khazmovsky on 2016/11/9.
//  Copyright © 2016年 Xuan Yi Xia CO.LTD. All rights reserved.
//

#import <UIKit/UIKit.h>
#import "YXVideoComponentVideo.h"
#import "YXVideoEffectModel.h"
#import "YXVideoPasterObject.h"
#import "YXVideoEffect.h"
#import "YXVideoTextModel.h"
#import "YXGLContext.h"
#import "YXPersistenceProcessEditor.h"

@class YXVideoSDKDriver;

@protocol YXVideoSDKDriverDelegate <NSObject>


/**
 播放回调

 @param driver driver实例
 @param time 播放当前时间
 @param duration 总时间
 */
- (void)driver:(YXVideoSDKDriver *)driver played:(CGFloat)time total:(CGFloat)duration;



/**
 开始播放回调

 @param driver driver实例
 */
- (void)driverDoPlay:(YXVideoSDKDriver *)driver;


/**
 暂停播放回调

 @param driver driver实例
 */
- (void)driverDoPause:(YXVideoSDKDriver *)driver;


/**
 完成整个视频播放回调

 @param driver driver实例
 */
- (void)driverPlayback:(YXVideoSDKDriver *)driver;


/**
 加载视频播放失败回调, 需要重新加载

 @param driver driver实例
 */
- (void)driverLoadVideoFailed:(YXVideoSDKDriver *)driver;


/**
 变声服务开始

 @param driver driver
 */
- (void)driverVoiceChangeServiceStarted:(YXVideoSDKDriver *)driver;

/**
 变声服务完成
 
 @param driver driver
 */
- (void)driverVoiceChangeServiceDone:(YXVideoSDKDriver *)driver;

/**
 变声服务取消
 
 @param driver driver
 */
- (void)driverVoiceChangeServiceCancelled:(YXVideoSDKDriver *)driver;

@end

@interface YXVideoSDKDriver : NSObject


/**
 预览 View
 */
@property (nonatomic, readonly) UIView *renderView;



/**
 预览 背景清除色
 */
@property (nonatomic, strong) UIColor *renderViewClearColor;


/**
 加载的视频长度
 */
@property (nonatomic, readonly) CGFloat videoDuration;


@property (nonatomic, readonly) AVComposition *composition;
@property (nonatomic, readonly) AVVideoComposition *videoComposition;
@property (nonatomic, readonly) AVAudioMix *audioMix;

/**
 当前播放时间
 */
@property (nonatomic, readonly) CGFloat currentPlayingTime;


/**
 是否正在播放
 */
@property (nonatomic, readonly) BOOL isPlaying;


/**
 加载的视频尺寸
 */
@property (nonatomic, readonly) CGSize videoSize;


/**
 是否开启循环播放
 */
@property (nonatomic, assign  ) BOOL repeatPlayback;
@property (nonatomic, weak    ) id<YXVideoSDKDriverDelegate> delegate;
@property (nonatomic, strong  ) NSString *userNickName;


/**
 导出视频目标尺寸
 */
@property (nonatomic, assign)   CGSize tarVidSize;


/**
 选中的视频范围
 */
@property (nonatomic, assign) CMTimeRange videoRange;


/**
 driver 初始化

 @param videoClips 视频片段数组
 @return driver实例
 */
- (id)initWithVideoClips:(NSArray <YXVideoComponentVideo *> *)videoClips;

- (id)initWithComposition:(AVComposition *)composition videoComposition:(AVVideoComposition *)videoComposition audioMix:(AVAudioMix *)audioMix;


/**
 加载效果 model

 @param videoEffectModel 效果 model
 @param musicPath 如果给视频陪背景音乐 音乐的路径
 @param timeRange 音乐循环区间
 @param doneHandler 完成加载 model回调
 */
- (void)loadVideoEffectModel:(YXVideoEffectModel *)videoEffectModel musicPath:(NSString *)musicPath trackRange:(CMTimeRange)timeRange whenDone:(void (^)())doneHandler;


- (void)loadVideoEffectModel:(YXVideoEffectModel *)videoEffectModel whenDone:(void (^)())doneHandler;


/**
 视频原声开关

 @param enable 是否开启
 */
- (void)enableOriginalSound:(BOOL)enable;

// 字幕
- (void)updateSubtitles:(NSArray <NSDictionary <NSString *, NSString *> *> *)subtitles;

// 贴纸

/**
 添加一个贴纸

 @param paster 贴纸实例
 */
- (void)addPaster:(YXVideoPasterObject *)paster;


/**
 移除一个贴纸

 @param paster 贴纸实例
 */
- (void)removePaster:(YXVideoPasterObject *)paster;


/**
 获取全部已经添加的贴纸

 @return 贴纸数组
 */
- (NSArray <YXVideoPasterObject *> *)getAddedPasters;


/**
 更新贴纸层
 */
- (void)updatePasterLayer;


/**
 隐藏贴纸层
 */
- (void)hidePasterLayer;


/**
 显示贴纸层
 */
- (void)showPasterLayer;

// 音乐
- (void)setMusicVolume:(CGFloat)volume; //背景
- (void)setOriginVolume:(CGFloat)volume; //原音

// layer
- (void)arrangeTextLayerWithText:(NSString *)text
                       textColor:(UIColor *)textColor
                 backgroundColor:(UIColor *)backgroundColor
                            font:(UIFont *)font
                        position:(CGPoint)position
                       transform:(CGAffineTransform)transform
                          forKey:(NSString *)key;

- (void)arrangeLayer:(CALayer *)layer forKey:(NSString *)key;
- (void)removeLayerWithKey:(NSString *)key;

// 字幕


/**
 添加一个字幕 Model

 @param textModel model
 */
- (void)arrangeTextModel:(YXVideoTextModel *)textModel;


/**
 移除一个字幕 Model

 @param textModel model
 */
- (void)removeTextModel:(YXVideoTextModel *)textModel;


/**
 移除 index位置上的 model

 @param index index
 */
- (void)removeTextModelAtIndex:(NSUInteger)index;


/**
 移除所有 model
 */
- (void)removeAllTextModel;


/**
 显示已经加载的 model
 */
- (void)showArrangedTextModel;


/**
 隐藏所有 model
 */
- (void)hideArrangedTextModel;




// 变声

/**
 变声控制
 
 @param pitch 音调
 @param speed 语速
 */
- (void)suitVideoWithPitch:(NSInteger)pitch speed:(CGFloat)speed;

// 控制


/**
 播放
 */
- (void)play;


/**
 暂停
 */
- (void)pause;


/**
 seek

 @param second seek到多少秒
 */
- (void)seekToSecond:(CGFloat)second;


/**
 seek

 @param progress seek到百分比
 */
- (void)seekToProgress:(CGFloat)progress;

- (void)seekToSecondWithHandleDecode:(CGFloat)second;
- (void)seekToProgressWithHandleDecode:(CGFloat)progress;

- (void)seekServiceDone;

// 导出

/**
 导出方法

 @param path 导出路径
 @param fileType 文件 type 一般 mp4使用 AVFileTypeMPEG4
 @param bitRate 导出文件的比特率
 @param exportingBlock 正在导出的回调
 @param exportedBlock 导出完成的回调
 @param exportCanceledBlock 导出取消的回调
 @param exportFailed 导出失败的回调
 */
- (void)exportVideoToPath:(NSString *)path fileType:(NSString *)fileType bitRate:(NSInteger)bitRate exporting:(void (^)(CGFloat progress))exportingBlock exported:(void (^)())exportedBlock exportCanceled:(void (^)())exportCanceledBlock exportFailed:(void (^)(NSError *error))exportFailed;


/**
 取消导出
 */
- (void)cancelExport;


@property (nonatomic, copy) void (^SeekWithTextureCallback)(GLuint texture, GLuint width, GLuint height);

- (void)updatef:(float)value forName:(NSString *)name;

- (void)runSyncInRenderQueue:(void (^)())block;
- (void)runAsyncInRenderQueue:(void (^)())block;

@property (nonatomic, readonly) YXGLContext *context;

@end


@interface YXVideoSDKDriver(Thumbnail)

- (void)GetThumbnailsWithCount:(NSInteger)count scaled:(CGFloat)scale compeletion:(void (^)(NSArray <UIImage *> *))callback;
- (void)GetThumbnailsWithCount:(NSInteger)count scaled:(CGFloat)scale compeletionC:(void (^)(UIImage *image, NSUInteger index))callback;

@end

// persistence
@interface YXVideoSDKDriver (Persistence)

@property (nonatomic, readonly) NSArray *persistenceEditors;

- (YXPersistenceProcessEditor *)persistenceFilterForName:(NSString *)name;
- (void)loadPersistenceFilterModel:(YXVideoEffectModel *)model;

@end

